package com.finaInorderProvider.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.finaInorderProvider.mapper.FinaInorderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.*;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.financial.FinaInorderService;
import util.CropStringUtil;
import util.SnowIdUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author oahner
 * @create 2022-04-22 8:23
 */
@Service
public class FinaInorderServiceImpl implements FinaInorderService {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private FinaInorderMapper mapper;

    /**
     * 获取采购订单列表
     * @param finaInorderPage
     * @return
     */
    @Override
    public ResponseMessage getFinaInorder(Map<String, Object> finaInorderPage) {
        logger.info("FinaInorderServiceImpl getFinaInorder params:finaInorderPage="+finaInorderPage);
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        Inorder inorder = null;
        Integer pageNum = 1;
        //拆解Map，分别获取订单对象和分页参数
        if(null!=finaInorderPage.get("finaInorder")){
            inorder = JSONObject.parseObject(JSONObject.toJSONString(finaInorderPage.get("finaInorder")), Inorder.class);
            inorder = (Inorder) CropStringUtil.cropString(inorder);
        }
        if(null!=finaInorderPage.get("pageNum") && (Integer)finaInorderPage.get("pageNum")>0){
            pageNum = (Integer) finaInorderPage.get("pageNum");
        }
        try {
            PageHelper.startPage(pageNum,5);
            List<Inorder> finaInorder = mapper.selFinaInorderList(inorder);
            if(finaInorder.size()>0){
                PageInfo<Inorder> pageOrder = new PageInfo<Inorder>(finaInorder);
                msg.setCode(StatusCode.OK);
                msg.setFlag(true);
                msg.setData(pageOrder);
                msg.setMessage("get finaInorder success");
                logger.debug("FinaInorderServiceImpl getFinaInorder success result:msg="+msg);
            }else{
                msg.setMessage("get finaInorder empty");
                logger.debug("FinaInorderServiceImpl getFinaInorder empty result:msg="+msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("get finaInorder error");
            logger.error("FinaInorderServiceImpl getFinaInorder error param:msg="+msg);
        }
        return msg;
    }

    /**
     * 获取采购订单详情
     * @param finaInorderinfo
     * @return
     */
    @Override
    public ResponseMessage getFinaInorderInfo(Inorderinfo finaInorderinfo) {
        logger.info("FinaInorderServiceImpl getFinaInorderInfo params:finaInorderinfo="+finaInorderinfo);
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        try {
            if(null != finaInorderinfo && null!=finaInorderinfo.getoId() && !"".equals(finaInorderinfo.getoId())){
                List<Inorderinfo> finaInorderinfos = mapper.selFinaInorderinfo(finaInorderinfo);
                if(finaInorderinfos.size()>0){
                    msg.setCode(StatusCode.OK);
                    msg.setFlag(true);
                    msg.setData(finaInorderinfos);
                    msg.setMessage("get finaInorderinfo success");
                    logger.debug("FinaInorderServiceImpl getFinaInorderInfo success result:msg="+msg);
                }else{
                    msg.setMessage("get finaOutorderinfo empty");
                    logger.debug("FinaInorderServiceImpl getFinaInorderInfo empty result:msg="+msg);
                }
            }else{
                msg.setMessage("get finaOutorderinfo fail");
                logger.debug("FinaInorderServiceImpl getFinaInorderInfo fail result:msg="+msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("get finaOutorderinfo error");
            logger.error("FinaInorderServiceImpl getFinaInorderInfo error param:msg="+msg);
        }
        return msg;
    }

    /**
     * 采购订单支付
     * @param inorder
     * @return
     */
    @Override
    public ResponseMessage payInorder(Inorder inorder) {
        logger.info("FinaInorderServiceImpl payInorder params:inorder="+inorder);
        ResponseMessage msg = new ResponseMessage();
        msg.setFlag(false);
        msg.setCode(StatusCode.ERROR);
        try {
            //判断订单号是否存在
            if(null != inorder && !"".equals(inorder.getOrderNum())){
                inorder.setStatus(1);
                inorder.setUpdateUser("RENHAO");
                int updateRow = mapper.updateFinaInorder(inorder);
                if(updateRow > 0){
                    msg.setCode(StatusCode.OK);
                    msg.setFlag(true);
                    msg.setData(updateRow);
                    msg.setMessage("pay order success");
                    logger.debug("FinaInorderServiceImpl payInorder success result:msg="+msg);
                }else{
                    msg.setMessage("pay order empty");
                    logger.debug("FinaInorderServiceImpl payInorder empty result:inorder="+inorder);
                }
            }else{
                msg.setMessage("pay order fail");
                logger.debug("FinaInorderServiceImpl payInorder fail result:inorder="+inorder);
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setMessage("pay order error");
            logger.error("FinaInorderServiceImpl payInorder error param:inorder="+inorder);
        }
        return msg;
    }

    /**
     * 增加财务账单
     * @param bill
     */
    public void addBill(Bill bill){
        logger.info("FinaInorderServiceImpl addBill params:bill="+bill);
        int i = 0;
        if(null != bill && !StringUtils.isEmpty(bill.getInId())){
            bill.setId(String.valueOf(SnowIdUtil.getSnowflakeId()));
            bill.setStatus(0);
            bill.setOutId("0");
            bill.setCreateUser("RENHAO");
            bill.setUpdateUser("RENHAO");
            i = mapper.insertBill(bill);
        }
        logger.debug("FinaInorderServiceImpl addBill result:total="+i);
    }



}
